#pragma once

#include <iostream>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

using namespace std;

#define SIZE 1024

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define Screen 1
#define Onefile 2
#define Classfile 3

#define Logfile "log.txt"

class Log
{
public:
    Log()
    {
        printMethod=Screen;
        path="./log/";
    }
    void Enable(int method)
    {
        printMethod=method;
    }
    std::string levelToString(int level)
    {
        switch (level)
        {
        case Info:
            return "Info";
        case Debug:
            return "Debug";
        case Warning:
            return "Warning";
        case Error:
            return "Error";
        case Fatal:
            return "Fatal";
        default:
            return "None";
        }
    }
    void logmessage(int level, char *format, ...)
    {
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftbuffer[SIZE];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
                 ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        va_list s;
        va_start(s, format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);
        char logtxt[SIZE * 2];
        snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer);
        printLog(level,logtxt);
        // printf("%s\n", logtxt);
    }
    void printLog(int level,std::string logtxt)
    {
        switch(printMethod)
        {
            case Screen:
                std::cout<<logtxt<<std::endl;
            break;
            case Onefile:
                printOnefile(Logfile,logtxt);
            break;
            case Classfile:
                printClassfile(level,logtxt);
            break;
            default:
            break;
        }
    }
    void printOnefile(const string &logname,const string &logtxt)
    {
        string _logname= path + logname;
        int fd=open(_logname.c_str(),O_WRONLY|O_CREAT|O_APPEND,0666);
        if(fd<0) return;
        write(fd,logtxt.c_str(),logtxt.size());
        close(fd);
    }
    void printClassfile(int level,const string &logtxt)
    {
        string filename=Logfile;
        filename+=".";
        filename+=levelToString(level);  //"log.txt.Debug/Fatal/..."
        printOnefile(filename,logtxt);
    }
    ~Log()
    {}

    void operator()(int level,const char* format,...)
    {
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftbuffer[SIZE];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
                 ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        va_list s;
        va_start(s, format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);
        char logtxt[SIZE * 2];
        snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer);
        printLog(level,logtxt);
        // printf("%s\n", logtxt);
    }
private:
    int printMethod;
    std::string path;   //新文件路径
};

// int sum(int n,...)     //可变参数
// {
//     va_list s;   //va_list类似于char*
//     va_start(s,n);   //s=&n+1

//     int sum=0;
//     while(n--)
//     {
//         sum+=va_arg(s,int);
//     }

//     va_end(s); //s=NULL
//     return sum;
// }